package arithmetic;

import java.util.HashSet;
import java.util.Scanner;

public class TestHW41 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num1 = Integer.parseInt(sc.nextLine());
        int num2 = Integer.parseInt(sc.nextLine());
        String[] strings = new String[num2];
        int index = 0;
        while (sc.hasNextLine()) {
            strings[index] = sc.nextLine();
            index++;
        }
        HashSet<Integer> hashSet = getTopNum(strings, num1);
        // System.out.println(hashSet.toString());
        int len = getLen(strings, 1, hashSet);
        System.out.println(len);
    }

    public static int getLen(String[] strings, int len, HashSet<Integer> hashSet) {

        HashSet<Integer> hashSet2 = new HashSet<>();
        for (Integer subTop :
                hashSet) {
            int index = 0;
            while (index < strings.length) {
                String[] strs = strings[index].split(" ");
                int num1 = Integer.parseInt(strs[0]);
                int num2 = Integer.parseInt(strs[1]);
                if (subTop == num1) {
                    hashSet2.add(num2);
                }
                index++;
            }
        }
        if (hashSet2.size() > 0) {
            len++;
        } else {
            return len;
        }
        return getLen(strings, len, hashSet2);
    }

    //获取最顶端的数据
    public static HashSet<Integer> getTopNum(String[] strings, int num1) {
        int index = 0;
        HashSet<Integer> hashSet = new HashSet<>();
        while (index < strings.length) {
            String[] strings1 = strings[index].split(" ");
            int num = Integer.parseInt(strings1[1]);
            hashSet.add(num);
            index++;
        }

        HashSet<Integer> hashSet1 = new HashSet<>();
        for (int i = 0; i < num1; i++) {
            if (!hashSet.contains(i)) {
                hashSet1.add(i);
            }
        }
        return hashSet1;
    }
}
